#ifndef _SMARTPOINTER_H
#define _SMARTPOINTER_H

#include "Object.h"

namespace demolib
{
template<typename T>
class SmartPointer : public Object
{
private:
    T* m_pointer;
public:
    SmartPointer(T* pointer = nullptr) : m_pointer(pointer){ }
    SmartPointer(const SmartPointer<T>& obj)
    {
        m_pointer = obj.m_pointer;
        const_cast<SmartPointer<T>&>(obj).m_pointer = nullptr;
    }
    SmartPointer<T>& operator = (const SmartPointer<T>& obj)
    {
        if(&obj != this)
        {
            delete m_pointer;
            m_pointer = obj.m_pointer;
            const_cast<SmartPointer<T>&>(obj).m_pointer = nullptr;
        }
        return *this;
    }

    T* operator -> ()
    {
        return m_pointer;
    }

    T& operator * ()
    {
        return *m_pointer;
    }

    bool isNull()
    {
        return (m_pointer == nullptr);
    }

    T* get()
    {
        return m_pointer;
    }

    ~SmartPointer()
    {
        delete m_pointer;
    }
};

}

#endif //_SMARTPOINTER_H